package com.wyp.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wyp.service.impl.QuestionsServiceImpl;
import com.wyp.service.impl.ScoresServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class RecordManController {
    @Autowired
    private QuestionsServiceImpl questionsService;

    @Autowired
    private ScoresServiceImpl scoresService;

    @RequestMapping("/record_manage.html")
    public String tologin(Model model,
                          @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "4") int pageSize
    ) {
        if (pageNum <= 0) {
            pageNum = 1;
        }
        //1.引入分页插件,pageNum是第几页，pageSize是每页显示多少条,默认查询总数count
        PageHelper.startPage(pageNum, pageSize);
        //2.紧跟的查询就是一个分页查询-必须紧跟.后面的其他查询不会被分页，除非再次调用PageHelper.startPage
        try {
            List<Map<String, Object>> scores = scoresService.qureyAllScores();
            //3.使用PageInfo包装查询后的结果,5是连续显示的条数,结果list类型是Page<E>
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(scores, pageSize);
            //4.使用model/map/modelandview等带回前端
            model.addAttribute("pageInfo", pageInfo);
        } finally {
            PageHelper.clearPage(); //清理 ThreadLocal 存储的分页参数,保证线程安全
        }
        return "/admin/record/record_manage";
    }

    @RequestMapping("/record_information.html/{id}")
    public String scoreinfo(
            @PathVariable("id") int scoreid,
            Model model
    ) {
        Map<String, Object> scores = scoresService.qureyScoresByScoresid(scoreid);
        String content = (String) scores.get("testpapercontent");
        String allanswer = (String) scores.get("yanswer");
        String[] questionsid = content.split(",");
        List<String> answer = Arrays.asList(allanswer.split(","));
        List<Map<String, Object>> Squestions = new ArrayList<>();
        List<Map<String, Object>> Mquestions = new ArrayList<>();
        List<Map<String, Object>> Jquestions = new ArrayList<>();
        for (String questionid : questionsid) {
            if (!questionid.equals("")) {
                Map<String, Object> question = questionsService.qureyQuestionsByid(Integer.parseInt(questionid));

                if ((int)question.get("type") == 1) {
                    Squestions.add(question);
                }
                if ((int)question.get("type") == 2) {
                    Mquestions.add(question);
                }
                if ((int)question.get("type") == 3) {
                    Jquestions.add(question);
                }
            }
        }
        List<String> Sanswer = answer.subList(0, Squestions.size());
        List<String> Manswer = answer.subList(Squestions.size(), Squestions.size() + Mquestions.size());
        List<String> t = answer.subList(Squestions.size() + Mquestions.size(), Squestions.size() + Mquestions.size() + Jquestions.size());
        List<Boolean> Janswer = new ArrayList<>();
        for (String s : t) {
            if (s.equals("1")) {
                Janswer.add(true);
            } else {
                Janswer.add(false);
            }
        }
        model.addAttribute("testpaper", scores);
        model.addAttribute("Squestions", Squestions);
        model.addAttribute("Mquestions", Mquestions);
        model.addAttribute("Jquestions", Jquestions);
        model.addAttribute("Sanswers", Sanswer);
        model.addAttribute("Manswers", Manswer);
        model.addAttribute("Janswers", Janswer);
        return "admin/record/record_information";
    }

    @RequestMapping("/record_qurey.html")
    public String toqureytestlibinfo(
            @Nullable @RequestParam(value = "qureyby") Integer qureyby,
            @Nullable @RequestParam("qureymessage") String qureymessage
    ) throws UnsupportedEncodingException {
        if (qureymessage != null && !(qureymessage.equals(""))) {
            qureymessage = new String(qureymessage.getBytes("utf-8"), "iso-8859-1");
            return "redirect:/admin/record_query.html/" + qureyby + "/" + qureymessage;
        } else {
            return "redirect:/admin/record_manage.html";
        }
    }

    @RequestMapping("/record_query.html/{qureyby}/{qureymessage}")
    public String qureynofinis(Model model,
                               @PathVariable(value = "qureyby") Integer qureyby,
                               @Nullable @PathVariable("qureymessage") String qureymessage,
                               @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "4") int pageSize
    ) {
        if (pageNum <= 0) {
            pageNum = 1;
        }
        //获取该考生已取得成绩的试卷
        //1.引入分页插件,pageNum是第几页，pageSize是每页显示多少条,默认查询总数count
        PageHelper.startPage(pageNum, pageSize);
        try {
            if (qureymessage == null || qureymessage.equals("")) {
                List<Map<String, Object>> scores = scoresService.qureyAllScores();
                PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(scores, pageSize);
                model.addAttribute("pageInfo", pageInfo);
            }
            if ((qureyby == 1) && (qureymessage != null)) {
                List<Map<String, Object>> scores = scoresService.qureyScoresByPapername(qureymessage);
                PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(scores, pageSize);
                model.addAttribute("pageInfo", pageInfo);
            }
            if ((qureyby == 0) && (qureymessage != null)) {
                List<Map<String, Object>> scores = scoresService.qureyScoresByStuname(qureymessage);
                PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(scores, pageSize);
                model.addAttribute("pageInfo", pageInfo);
            }
        } finally {
            PageHelper.clearPage(); //清理 ThreadLocal 存储的分页参数,保证线程安全
        }
        return "/admin/record/record_manage";
    }

    @GetMapping("/record_delete.html/{id}")
    public String todeletepaper(
            Model model,
            @PathVariable("id") int id
    ) {
        scoresService.deleteScores(id);
        return "redirect:/admin/record_manage.html";
    }
}
